﻿Imports System.Net 'To be able to use network related functions.
Imports System.IO

Public Class Updater

    Private Sub Updater_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' ADD THIS BETWEEN YOUR PUBLIC CLASS OR PRIVATE OR PUBLIC SUBS
        ' YOU MUST ALSO BE SURE TO IMPORT "SYSTEM.NET" & " SYSTEM.IO"
        ' IF YOU CHOOSE, INWHICH IT JUST BE WISE, BE SURE TO CHANGE Updater_Load TO MATCH YOUR FILE. ex: Form1_Load

        Dim web As New WebClient 'To be able to download the content from the latest version file you have stored.
        Dim LatestVersion As String = web.DownloadString("http://cryomods.com/prods/CLauncher-version.txt") 'To download the Lastest Version from a specified URL.
        Dim CurrentApp As String = My.Application.Info.Version.ToString 'Gets the applications current version

        If CurrentApp < LatestVersion Then 'If the applications current version is less than the Latest version Then it will update, otherwise just do nothing or w/e you want.

            If MessageBox.Show("A new update is available!" & vbNewLine & "Would you like to update?", "LE21 - Auto Updater", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then 'Message box dialog asking the user if they wish to update or not.
                My.Computer.Network.DownloadFile("http://YOURWEBSITE.COM/YOURFILE.exe", Application.StartupPath & "\NAME_OF_FOLDER\YOURFILE.exe") 'If they choose Yes, it will download the NEWEST version exe (BE SURE THE NAME IS THE NEWEST VERSION!)
                Label2.Text = "Downloading....."
                MsgBox("The update has been downloaded!" & vbNewLine & "The application will now restart.") 'telling the user the app will restart
                Application.Restart() 'exits application
                End
            Else
            End If

        Else

        End If
        If MessageBox.Show("Your softare is already running the latest!" & vbNewLine & "  You May Now Close This Window!", "Cryomod - Auto Updater", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) = Windows.Forms.DialogResult.OK Then 'Message box dialog letting the user know its safe to exit the updater.

        End If
        Me.Close()

    End Sub

End Class